package artid.com.arttogo.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyphenate.util.ImageUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.ShowPhoto;
import artid.com.arttogo.view.HackyViewPager;
import artid.com.arttogo.view.photo.ImageDetailFragment;

//点击查看大图的页面
public class PhotoActivity extends FragmentActivity implements OnClickListener {

	private static final String STATE_POSITION = "STATE_POSITION";
	public static final String EXTRA_IMAGE_INDEX = "image_index";
	public static final String EXTRA_IMAGE_URLS = "image_urls";

	private HackyViewPager mPager;
	private int pagerPosition;
	private TextView indicator;
	private LinearLayout mLeft, mRight, mSave;
	private Context context;
	private ImageView back;
	private int width;
	TextView title, info;
	ShowPhoto showitem;
	List<ShowPhoto> listPhoto;
	ArrayList<String> listTitle = new ArrayList<String>(),listInfo = new ArrayList<String>();
	Boolean isShowFirstDotPic = false;//是否显示第一张微观图
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.activity_photo);
		title = (TextView) findViewById(R.id.text_photoname);
		info = (TextView) findViewById(R.id.text_photoinfo);
		back = (ImageView) findViewById(R.id.back_btn);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		initImageLoader();
		
		pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);

		showitem = (ShowPhoto) getIntent().getSerializableExtra("item");

		if (showitem == null) {
			return;
		}

		String[] urls = null;
		if (showitem.getList() == null) {
			if (!TextUtils.isEmpty(showitem.getUrl())) {
				urls = showitem.getUrl().split("@@");
			} else {
				return;
			}
			if (!TextUtils.isEmpty(showitem.getName())) {
				title.setText(showitem.getName());
			}
			if (!TextUtils.isEmpty(showitem.getInfo())) {
				info.setText(showitem.getInfo());
			}

		}
		if (showitem.getList() != null) {
			listPhoto = showitem.getList();
			String allurlstr = "";
			for (int i = 0; i <listPhoto.size(); i++) {
				allurlstr+=(listPhoto.get(i).getUrl()+"@@");
				listInfo.add(listPhoto.get(i).getInfo());
				listTitle.add(listPhoto.get(i).getName());
			}
			title.setText(listTitle.get(0));
			info.setText(listInfo.get(0));
			urls = allurlstr.split("@@");

			// ShowPhoto item = showitem.getList();

		}

		//
		// String src = getIntent().getStringExtra("src");
		// String strtitle = getIntent().getStringExtra("tittle");
		// if (TextUtils.isEmpty(src))
		// return;
		// String[] urls = {src};
		// // String[] strtitles = {strtitle};
		// if (!TextUtils.isEmpty(strtitle)) {
		// title.setText(strtitle);
		// }

		// String[] urls = {
		// "http://124.205.21.164:8888/test/bd2.png",
		// "http://img.redocn.com/sheying/20130504/huangnitudiwenbeijingtu_2080792_small.jpg",
		// // "http://i3.tietuku.com/f43385c2f9867acf.jpg",
		// "http://img4.duitang.com/uploads/item/201406/28/20140628082128_F4mmL.thumb.700_0.jpeg"
		// };
		WindowManager wm = this.getWindowManager();
		width = wm.getDefaultDisplay().getWidth();

		mPager = (HackyViewPager) findViewById(R.id.pager);
		ImagePagerAdapter mAdapter = new ImagePagerAdapter(
				getSupportFragmentManager(), urls);
		mPager.setAdapter(mAdapter);
		indicator = (TextView) findViewById(R.id.indicator);

		CharSequence text = getString(R.string.viewpager_indicator, 1, mPager
				.getAdapter().getCount());
		if (mPager.getAdapter().getCount()>0) {
			indicator.setVisibility(View.VISIBLE);
		}
		indicator.setText(text);
		// 更新下标
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageSelected(int arg0) {
				CharSequence text = getString(R.string.viewpager_indicator,
						arg0 + 1, mPager.getAdapter().getCount());
				indicator.setText(text);
				title.setText(listTitle.get(arg0));
				info.setText(listInfo.get(arg0));
			}

		});
		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}

		mPager.setCurrentItem(pagerPosition);
		mSave = (LinearLayout) findViewById(R.id.save);
		mLeft = (LinearLayout) findViewById(R.id.left);
		mRight = (LinearLayout) findViewById(R.id.right);
		mSave.setOnClickListener(this);
		mLeft.setOnClickListener(this);
		mRight.setOnClickListener(this);

	}

	public static String getZoomUrl(String url) {
		if (url.contains("_samll"))
			return url.replace("_samll", "");
		else
			return url;
	}

	private void initImageLoader() {
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.defaultDisplayImageOptions(defaultOptions)
				.discCacheSize(50 * 1024 * 1024)//
				.discCacheFileCount(100)// 缓存一百张图片
				.writeDebugLogs().build();
		ImageLoader.getInstance().init(config);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mPager.getCurrentItem());
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter {

		public String[] fileList;

		public ImagePagerAdapter(FragmentManager fm, String[] fileList) {
			super(fm);
			this.fileList = fileList;
		}

		@Override
		public int getCount() {
			return fileList == null ? 0 : fileList.length;
		}

		@Override
		public ImageDetailFragment getItem(int position) {
			String url = fileList[position];
			return ImageDetailFragment.newInstance(url);
		}

		@Override
		public ImageDetailFragment instantiateItem(ViewGroup container,
				int position) {
			return (ImageDetailFragment) super.instantiateItem(container,
					position);
		}
	}

	/**
	 * 左旋转
	 * 
	 * @param imageView
	 */
	private void left(ImageView imageView) {
		Bitmap bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
		if (null == bmp)
			return;

		bmp = ImageUtils.rotateImageView(-90, bmp);

		imageView.setImageBitmap(bmp);
	}

	/**
	 * 右旋转
	 * 
	 * @param imageView
	 */
	private void right(ImageView imageView) {
		Bitmap bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
		if (null == bmp)
			return;
		bmp = ImageUtils.rotateImageView(90, bmp);
		imageView.setImageBitmap(bmp);
	}

	/**
	 * 保存图片
	 * 
	 */
	// private void save(ImageView imageView) {
	// try {
	// Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
	// if (null == bm) {
	// Config.showTip(this, "图片保存失败");
	// return;
	// }
	// ImageUtils.saveImage(context, bm,
	// "img" + System.currentTimeMillis() + ".jpg");
	// Config.showTip(this, "图片保存成功");
	// } catch (IOException e) {
	// Config.showTip(this, "图片保存失败");
	// }
	// }

	@Override
	public void onClick(View v) {
//		ImageView imageView = ((ImagePagerAdapter) mPager.getAdapter())
//				.instantiateItem(mPager, mPager.getCurrentItem())
//				.getView();
//		switch (v.getId()) {
//		case R.id.left:
//			if (null != imageView)
//				left(imageView);
//			break;
//		case R.id.right:
//			if (null != imageView)
//				right(imageView);
//			break;
//		// case R.id.save:
//		// if (null != imageView)
//		// save(imageView);
//		//
//		// break;
//
//		default:
//			break;
//		}

	}
}
